﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace MM.Common.DBBase
{
    internal class LocalLogger : LogBase
    {
        // Fields
        private string _expPath;
        private int _folderLoop;
        private string _logPath;
        private int _writeLoop;
        private const int MAX_LOOP = 3;

        // Methods
        public LocalLogger()
            : this("", "", "")
        {
        }

        public LocalLogger(string logname, string objectname, string eventname)
        {
            this._logPath = "";
            this._expPath = "";
            base._logName = logname;
            base._objectName = objectname;
            base._eventName = eventname;
            this.CreateLogFile();
            this.CreateExpFile();
        }

        private void CreateExpFile()
        {
            this._expPath = AppDomain.CurrentDomain.BaseDirectory;
            try
            {
                this._expPath = this._expPath + "App_Data";
                if (!Directory.Exists(this._expPath))
                {
                    Directory.CreateDirectory(this._expPath);
                }
                this._expPath = this._expPath + @"\log";
                if (!Directory.Exists(this._expPath))
                {
                    Directory.CreateDirectory(this._expPath);
                }
                this._expPath = this._expPath + @"\" + this.GetFolderName();
                if (!Directory.Exists(this._expPath))
                {
                    Directory.CreateDirectory(this._expPath);
                }
                this._expPath = this._expPath + @"\" + this.GetExpFileName();
                if (!File.Exists(this._expPath))
                {
                    File.CreateText(this._expPath).Close();
                }
            }
            catch (Exception exception)
            {
                if (this._folderLoop > 3)
                {
                    this._folderLoop = 0;
                    throw new Exception(exception.Message + " 创建异常日志文件失败。", exception);
                }
                Thread.Sleep(200);
                this._folderLoop++;
                this.CreateExpFile();
            }
            finally
            {
                this._folderLoop = 0;
            }
        }

        private void CreateLogFile()
        {
            this._logPath = AppDomain.CurrentDomain.BaseDirectory;
            try
            {
                this._logPath = this._logPath + "App_Data";
                if (!Directory.Exists(this._logPath))
                {
                    Directory.CreateDirectory(this._logPath);
                }
                this._logPath = this._logPath + @"\log";
                if (!Directory.Exists(this._logPath))
                {
                    Directory.CreateDirectory(this._logPath);
                }
                this._logPath = this._logPath + @"\" + this.GetFolderName();
                if (!Directory.Exists(this._logPath))
                {
                    Directory.CreateDirectory(this._logPath);
                }
                this._logPath = this._logPath + @"\" + this.GetLogFileName();
                if (!File.Exists(this._logPath))
                {
                    File.CreateText(this._logPath).Close();
                }
            }
            catch (Exception exception)
            {
                if (this._folderLoop > 3)
                {
                    this._folderLoop = 0;
                    throw new Exception(exception.Message + " 创建日志文件失败。", exception);
                }
                Thread.Sleep(200);
                this._folderLoop++;
                this.CreateLogFile();
            }
            finally
            {
                this._folderLoop = 0;
            }
        }

        private string GetExpFileName()
        {
            string str = "";
            if (base._logName != string.Empty)
            {
                str = base._logName + "_";
            }
            DateTime now = DateTime.Now;
            return string.Concat(new object[] { str, now.Year, "-", now.Month, "-", now.Day, ".exp" });
        }

        private string GetFolderName()
        {
            DateTime now = DateTime.Now;
            return (now.Year + "-" + now.Month);
        }

        private string GetLogFileName()
        {
            string str = "";
            if (base._logName != string.Empty)
            {
                str = base._logName + "_";
            }
            DateTime now = DateTime.Now;
            object obj2 = str;
            return string.Concat(new object[] { obj2, now.Year, "-", now.Month, "-", now.Day, ".log" });
        }

        private string GetTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }

        private void SaveLog(string logOrExp, string path, string loginfo)
        {
            if (((logOrExp != "LOG") || base.EnableLog) && ((logOrExp != "EXP") || base.EnableExp))
            {
                try
                {
                    if (File.Exists(path))
                    {
                        TextWriter writer = File.AppendText(path);
                        string str = "[" + this.GetTime() + "] ";
                        if (base._operId != 0)
                        {
                            str = str + " 操作者:" + this._operId.ToString();
                        }
                        if (base._computerId != "")
                        {
                            str = str + " 机器码:" + base._computerId;
                        }
                        if (base._systemCode != "")
                        {
                            str = str + " 系统代码:" + base._systemCode;
                        }
                        if (base._objectName != "")
                        {
                            str = str + " 对象:" + base._objectName;
                        }
                        if (base._eventName != "")
                        {
                            str = str + " 事件:" + base._eventName;
                        }
                        str = str + "\r\n" + loginfo + "\r\n";
                        writer.WriteLine(str);
                        writer.Flush();
                        writer.Close();
                        writer = null;
                    }
                }
                catch (Exception exception)
                {
                    if (this._writeLoop > 3)
                    {
                        this._writeLoop = 0;
                        throw new Exception(exception.Message + " 日志文件写入失败。", exception);
                    }
                    this.SaveLog(logOrExp, path, loginfo);
                    this._writeLoop++;
                }
            }
        }

        public override void WriteExp<T>(T exp)
        {
            this.SaveLog("EXP", this._expPath, exp.ToString());
        }

        public override void WriteExp(string objectname, string eventname, string info)
        {
            string loginfo = "对象:" + objectname + ", 事件:" + eventname + "," + info;
            this.SaveLog("EXP", this._expPath, loginfo);
        }

        public override void WriteLog<T>(T info)
        {
            this.SaveLog("LOG", this._logPath, info.ToString());
        }

        public override void WriteLog(LogType type, string info)
        {
            string loginfo = type.ToString() + ":" + info;
            this.SaveLog("LOG", this._logPath, loginfo);
        }
    }
}
